	//初始化子资源
	refPage =function (){
		initChilds();
	}
			
	function childResClick(ul){
	    var uls = $("ul[name='resUl']");
	    $.each(uls,function(index,ob){
	    	var selector =$(ob).attr('selector');
	    	if(selector!=null&&selector!=''&&selector!='undefined'&&selector=='true'){
	    		$(ob).css("background-color",'');
	    	}
	    });
		$(ul).attr('selector','true');
		$(ul).css('background-color','#eeeff1');
	}
			
	//弹出新增父资源关系窗口
	function openResRelationWindow(){
		if(isDel(initResId)==true){
	  		getTopWin(window).getMessageBox().alert('资源已被删除');
	  		return;
	  	}
	  
		var url ='iframe:resource/res-relation!input.action?parentId='+initResId;
		var opt =  {title:'添加父子关系',id:'resRelationWindow',dragLimit:false,width:560,height:380,iframeScrolling:'no',submit:function(v, h, f){},buttons:{}};
		getTopWin(window).openjBox(url,opt,'resRelationWindow',window);
	}
	
	//验证是否删除
	 function isDel(id){
			var isFlag = false;
			$.ajax({
					type:'post',
					url:'res!isDelete.action',
					data:{id:id},
					global:false,
					async:false,
					cache:false,
					dataType:'json',
					success:function(data,textStatus){
						isFlag = data;
					},
					error:function(){
						getTopWin(window).getMessageBox().alert('检验属性是否删除出现异常!','错误');
					}
			});
			return isFlag;
	}
			
function deleteChildRes(deleteid,isSelf){
		  if(deleteid==null){
		  	 var uls = $("ul[name='resUl']");
		  	 $.each(uls,function(index,ob){
		    	var selector =$(ob).attr('selector');
		    	if(selector!=null&&selector!=''&&selector!='undefined'&&selector=='true'){
		    		deleteid =$(ob).attr('resId');
		    		return false;
		    	}
		    });
		    
		    if(deleteid==null){
		  	 	getTopWin(window).getMessageBox().alert('请选择一个资源','提示');
		  	 	return;
		  	 }
		  }
		  
	  var submit = function(v, h, f){
	  	if (v == 'ok'){
	  		 if(isDel(deleteid)==true){
			  	getTopWin(window).getMessageBox().alert('资源已被删除');
			  	initChilds();
			  	return;
	 		  }
	  		
	  		getTopWin(window).getMessageBox().tip("删除资源中...", 'loading');
				var myparam={};
				if(isSelf==1){
					myparam = {"id":deleteid,"viewId":viewId}
				}else{
					myparam = {"id":deleteid,"pId":initResId}
				}
	  		$.ajax({
				type:'post',
				url:'../resource/res!delete.action',
				data:myparam,
				global:false,
				async:false,
				cache:false,
				dataType:'json',
				success:function(data,textStatus){
					if(isSelf!=1){ //删除子资源调用
						getTopWin(window).getMessageBox().tip('删除资源成功', 'success'); 
						initChilds();
						var tree = window.parent.getResTree();
				 		var selectedNode = tree.getSelectedNodes()[0];
				 		tree.reAsyncChildNodes(selectedNode, "refresh");
					}else{
						window.setTimeout(function () {
							getTopWin(window).getMessageBox().tip('删除资源成功', 'success'); 
				 			window.parent.location.reload();
						},1500);
					}
				},
				error:function(){
					getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
				}
			});
	  	}
	  }
	  getTopWin(window).getMessageBox().confirm('确定删除?','提示',submit);
 }
 
	 function initUpdate(id,pId,viewId){
	 	if(isDel(id)==true){
		  	getTopWin(window).getMessageBox().alert('资源已被删除');
		  	return;
		}
		
		location.assign('../resource/res!initUpdate.action?id='+id+'&pId='+pId+'&viewId='+viewId+'');
	 }


function initChilds(){
			$('#childs').html("");
			$.ajax({
				type:'post',
				url:'res-query!children.action',
				data:{"parentResId":initResId},
				global:false,
				async:false,
				cache:false,
				dataType:'json',
				success:function(data,textStatus){
					var sum=0;
					if(data!=null&&data.length>0){
							var html ="";
						$.each(data,function(index,ob){
							 html+="<ul class='xUl'>";
							var resTypeName = ob.resTypeName;
							var childNodes = ob.childNodes;
							html+="<li style='color:blue;text-indent: 6px; text-align:left;'>"+resTypeName+"("+childNodes.length+")"+"</li>";
							html+="<li>";
							$.each(childNodes,function(index,ob){ //遍历子节点
								html+="<ul name='resUl' class='xUl-ul' style='float:left;width: auto;' onclick='childResClick(this)' resId='"+ob.id+"'>";
								html+="<li style='text-align:center;'><img src='../style/default/tree/node/res1.png' /></li>";
								html+="<li style='text-align:center; margin-bottom:20px; word-break:break-all'><a target='_blank' href='res-query!view.action?id="+ob.id+"&pId=0&viewId=1&ntype=2' style='cursor:pointer;'>"+ob.name+"</a></li>";
								html+="</ul>";
								sum++;
							});
							html+="</li>";
							html+="</ul>";
						});
						$('#childs').append(html);
					}
					$('#childResCount').text("("+data.length+"个分类,"+sum+"个子节点)");
					
				},
				error:function(statu,error){
					getTopWin(window).getMessageBox().alert('子资源初始化失败!','错误');
				}
		 	});
}

	//得到当前选中的
		function fnGetSelected() {   
			var selections = $("input[name='eventCheck']:checked");
			return selections;
		}
		function datagrid(){
			var oTable=	$('#eventList').dataTable();
			oTable.fnPageChange("first",true);
		}
		function cancelAllCheck(){
			$("#dt_check_all").removeAttr("checked");
		}
		function batchAck(){
			var rows = fnGetSelected();
			if(rows.length>0){
				var id = new Array(rows.length);
				var oTable =$('#eventList').dataTable();
				for(var i = 0 ; i<rows.length;i++){
					id[i] = rows[i].value;
				}
				//校验是否有权限
				$.ajax({
					   type: "POST", 
					   url: "../event/active-event!handelAuth.action?id="+id, //请求的action
					   data: "", //传的参数
					   dataType: 'json',
					   success: function(data){ //结果
						   if(data == true){
							 //校验是否包含已认领事件
								$.ajax({
									   type: "POST", 
									   url: "../event/active-event!ackCheck.action?id="+id, //请求的action
									   data: $("#myform").serialize(), //传的参数
									   dataType: 'json',
									   success: function(data){ //结果
										   if(data == true){
											   	var url ="iframe:event/active-event!input.action?id="+id;
												var opt = { title: "批量认领活动事件",width:600,height:180,buttons: {}};
												getTopWin(window).openjBox(url,opt,'ack-active-event',window);
										   }else{
											   datagrid();
											   getTopWin(window).getMessageBox().alert("包含已认领或清除事件","提示");
										   }
									   }
								});
						   }else{
							   getTopWin(window).getMessageBox().alert("没有认领权限","提示");
						   }
					   }
				});
			}else{
				getTopWin(window).getMessageBox().alert("请选择一条数据","提示");
			}
	  }
	  function batchConfirm(){
			var rows = fnGetSelected();
			if(rows.length>0){
				var id = new Array(rows.length);
				var oTable =$('#eventList').dataTable();
				for(var i = 0 ; i<rows.length;i++){
					id[i] = rows[i].value;
				}
				//校验是否有权限
				$.ajax({
					   type: "POST", 
					   url: "../event/active-event!handelAuth.action?id="+id, //请求的action
					   data: "", //传的参数
					   dataType: 'json',
					   success: function(data){ //结果
						   if(data == true){
							 //校验是否包含已认领事件
							   $.ajax({
								   type: "POST", 
								   url: "../event/active-event!confirmCheck.action?id="+id, //请求的action
								   data: $("#myform").serialize(), //传的参数
								   dataType: 'json',
								   success: function(data){ //结果
									   if(data == true){
										   var url ="iframe:event/active-event!delete.action?id="+id;
											var opt = { title: "批量清除活动事件",width:600,height:280,buttons: {}};
											getTopWin(window).openjBox(url,opt,'confirm-active-event',window);
									   }else{
										   datagrid();
										   getTopWin(window).getMessageBox().alert("包含已清除事件","提示");
									   }
								   }
							});
						   }else{
							   getTopWin(window).getMessageBox().alert("没有清除权限","提示");
						   }
					   }
				});
			}else{
				getTopWin(window).getMessageBox().alert("请选择一条数据","提示");
			}
		}
	  function showMessage(message){
			window.setTimeout(function () { $.jBox.tip(message, 'success'); }, 100);
		}
		function eventDown(){
			var str=document.getElementsByName("level");
	    	var objarray=str.length;
	    	var level = new Array();
	    	var j = 0;
	    	for (i=0;i<objarray;i++)
	    	{
	    	  if(str[i].checked == true)
	    	  {
	    		  level[j++]=(str[i].value);
	    	  }
	    	}
	    	//状态
	    	var statusStr=document.getElementsByName("status");
	    	var statusArray=statusStr.length;
	    	var status = new Array();
	    	var k = 0;
	    	for (i=0;i<statusArray;i++)
	    	{
	    	  if(statusStr[i].checked == true)
	    	  {
	    		  status[k++]=(statusStr[i].value);
	    	  }
	    	}
			window.location="../event/event-export!doExport.action?level="+level+"&status="+status+"&resId="+initResId;
		}

function showEvent() {
	if ($("#top").css("display") == "none") {
		$("#top").show("normal");
	} else {
		$("#top").hide("normal");
	}
}

function showDataTable() {
	var oTable = $('#eventList').dataTable({
				"bProcessing" : true,
				"bServerSide" : true,
				"sServerMethod" : 'POST',
				"sAjaxSource" : '../event/active-event!resActiveEvents.action',
				"bPaginate" : true,
				"bSort" : true,
				"bFilter" : false,
				//"sScrollY":($(window).height()-$('#top').height())-118 ,
				"bScrollCollapse":true,
				"bJQueryUI" : false,
				"sPaginationType" : "full_numbers",
				"iDisplayLength" : 10,
				"sDom":'rt <"bottom"flpi>',
				"aLengthMenu" : [[10, 20, 50, 100], [10, 20, 50, 100]],
				"aoColumns" : [{"mDataProp" : "checkbox","bSortable" : false,"fnCreatedCell" : function(nTd, sData,oData, iRow,iCol) {
									$(nTd).html("<div style='text-align:center;'>"+sData+"</div>");
							  }},
							  {"mDataProp" : "statu","bSortable" : false,"fnCreatedCell" : function(nTd, sData,oData, iRow,iCol) {
									var statusName = "<div class='status"+oData.statu+"'></div>";
									var status= oData.statu;
									var html = "<a id='"+oData.id+"_s' title='"+oData.statuValue+"' >"+ statusName +"</a>";
									$(nTd).html(html);
							}}, 
							{"mDataProp" : "levelName","bSortable" : false,"fnCreatedCell" : function(nTd, sData,oData, iRow,iCol) {
								var html ="<a id='"+oData.id+"_l' title='"+sData+"' ><div class='eventlevel"+oData.level+"'></div></a>";
								$(nTd).html(html);
							}}, 
							{"mDataProp" : "msg","bSortable" : false,"fnCreatedCell" : function(nTd, sData,oData, iRow,iCol) {
								var html ="<a href='../event/event-view!view.action?id="+oData.id+"'target='_blank'>"+sData+"</a>";
								$(nTd).html(html);
							}},
						    {"mDataProp" : "eventTypeName","bSortable" : false},
						    {"mDataProp" : "resName","bSortable" : false},
						    {"mDataProp" : "occurTime","bSortable" : true}, 
						    {"mDataProp" : "id","bVisible" : false}, 
						    {"mDataProp" : "level","bVisible" : false}
							],
				"oLanguage" : {
					"sLengthMenu" : "每页 _MENU_ 条",
					"sZeroRecords" : "  ",
					"sInfo" : "当前从 _START_ 到 _END_ 条,共 _TOTAL_ 条记录",
					"sInfoEmpty" : "没有找到记录",
					"oPaginate" : {"sFirst" : "首页","sPrevious" : "上一页","sNext" : "下一页","sLast" : "尾页"
					}
				},
				"fnServerParams" : function(aoData) {
					aoData.push({"name" : "resId","value" : initResId});
					if(filterValue!=null){
						aoData.push({"name" : filterValue<=1?"FILTER_IN_Istatus":"FILTER_IN_Ilevel_","value" : filterValue});
					}
				},
				"fnInfoCallback":function( oSettings, iStart, iEnd, iMax, iTotal, sPre ) {
					var json =$.parseJSON(oSettings.jqXHR.responseText);
					$('#level2').text(json.level2==null?"":json.level2);
					$('#level3').text(json.level3);
					$('#level4').text(json.level4);
					$('#level5').text(json.level5);
					
					$('#claimOk').text(json.claimOk);
					$('#claimNo').text(json.claimNo);
					$('#allEvent').text(json.allEvent);
				}
			});
}

var filterValue =null ;
function onclickLevel(tempIndex){
	var arrayLevel = ["0","1","2","3","4","5"];
	filterValue = arrayLevel[tempIndex];
	var oTable = $('#eventList').dataTable();
	oTable.fnPageChange("first", true);
}

// 全选
function allClick() {
	var dt_check_all = $('#dt_check_all').attr("checked");
	var allCheckbox = $("input[name='eventCheck']");
	if (dt_check_all) {
		allCheckbox.attr("checked", allCheckbox); // 全选
		$('#eventList tbody tr').addClass('row_selected'); // 背景色全选
	} else {
		allCheckbox.attr("checked", false); // 取消全选
		$('#eventList tbody tr').removeClass('row_selected'); // 背景色取消
	}
}

function search() {
	var oTable = $('#eventList').dataTable();
	oTable.fnPageChange("first", true);
}

function getResValue(resId) {
	$.ajax({
				url : 'res-query!findResValue.action?id=' + resId + '&t='+ new Date().getMilliseconds(),
				global : false,
				async : false,
				dataType : "json",
				success : function(values) {
					fillRes(values);
				}
			});
}
var angularGaugeInit = false;
function showChart(healthDegree) {

	if (!angularGaugeInit) {
		var myChart = new FusionCharts("../resources/charts/AngularGauge.swf",
				"myChartId", 160, 80, "0", "0");
		var str = "<chart  bgColor='FFFFFF' showTickValues='1'  showBorder='0'  lowerLimit='0' fontcolor='000000' upperLimit='100' ";
		str += " majorTMColor='FFFFFF' majorTMAlpha='1' majorTMNumber='6' minorTMNumber='0' ";
		str += " tickValueStep='1'    tickValueDistance='10'   showTickMarks='1'  showBorder='0' basefontColor='000000' ";
		str += " chartTopMargin='2' chartBottomMargin='0' chartLeftMargin='8' chartRightMargin='15' ";
		str += " toolTipBgColor='80A905' > ";
		str += "<colorRange>";
		str += "<color minValue='0' maxValue='20' code='#ce110f' />";
		str += "<color minValue='20' maxValue='40' code='#ff9937' />";
		str += "<color minValue='41' maxValue='60' code='#dede03' />";
		str += "<color minValue='61' maxValue='80' code='#56a8f4' />";
		str += "<color minValue='81' maxValue='100' code='#2cb900' />";
		str += "</colorRange>";
		str += "<dials>";
		str += "<dial value = '" + healthDegree + "' rearExtension='10' />";
		str += "</dials>";
		str += "</chart>";
		myChart.setDataXML(str);
		myChart.render("chartdiv");  
		angularGaugeInit = true;
	} else {
		try {
			var chartRef = getChartFromId("myChartId");
			chartRef.setData(1, healthDegree);// 1: dial index, value
		} catch (e) {
		}
	}

}
function showCondition() {
	$("#searchFormDiv").toggle("fast");
}
function fillRes(values) {
	var res = values[0];
	var pres = values[3];
	var probe = values[4];
	if (pres == null) {
		$("#resname").html(res.name);
		$("#resname").attr("title",res.desc);
	} else {
		$("#resname").html(pres.name + "&nbsp;>>&nbsp;" + res.name);
	}
	var resType = values[1];
	$("#restypename").attr("href", "res-type!model.action?id=" + resType.id);
	$("#restypename").html(resType.name);
	$("#resid").html(res.id);
	$("#resip").html(res.ip);
	$("#rescode").html(res.code);
	$("#resinid").html(res.internalId);
	
	var probeStatuImg = "/bsm/style/default/bsm/images/proberun.png";
	if(probe.status!=3){
		probeStatuImg ="/bsm/style/default/bsm/images/probestop.png";
	}
	$("#resProbe").html(probe.name+"<img src='"+probeStatuImg+"' />");
	if (res.paused == 1) {
		$("#pausedStatusLi").show();
	} else {
		$("#pausedStatusLi").hide();
	}
	var attrs = values[2];
	$("#attUl li").each(function() {
		$(this).remove();
	});
	if (attrs.length == 0) {
		$("#attUl").append("<li><h3>没有可显示的配置属性</h3></li>");
	} else {
		$("#attUl").append("<li><h3></h3></li>");
	}
	for (var i = 0; i < attrs.length; i++) {
		$("#attUl").append("<li><span class='col-l-ul-l'>" + attrs[i].name
				+ "：</span><span class='col-l-ul-r'>" + (attrs[i].val==null?'':$.trim(attrs[i].val.replace("<","&lt;").replace(">","&gt;")))
				+ "</span></li>");
	}
	healthDegree = res.healthDegree;
	$("#resstatus").html(healthArr[res.healthStatus]);

	showChart(healthDegree);
	$('body').unmask();
}

refreshRes = function(resId) {
	$('body').mask("加载中...");
	if (flexInited) {
	
		initResId = resId;
		var oTable = $('#eventList').dataTable();
		oTable.fnPageChange("first", true);
	}
	setTimeout("getResValue(" + resId + ");", 100);
}
var flexInited = false;
function flexInitialized() {

	flexInited = true;
}


$(document).ready(function() {
			refreshRes(initResId);
			initChilds();
			showDataTable();
});
